﻿using SLClient.CustomerControls;
using SLClient.Extension;
using SLClient.ServiceAPI;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Threading;

namespace SLClient.Views
{
    public partial class NewsControlsView : UserControl
    {
        private ObservableCollection<Announment> _annnounment;
        private double _newsHeight = 0.0;
        private double _newsStackTop = 0.0;
        private int _posItem = 0;
        private readonly Storyboard sb;
        private DispatcherTimer showNewsListTimer;

        public NewsControlsView()
        {
            this.InitializeComponent();
            this.sb = new Storyboard();
            this._annnounment = new ObservableCollection<Announment>();
            if (IsolatedStorageHelper<ObservableCollection<Announment>>.IsSettingPersisted("News"))
            {
                this._annnounment = ViewDispatcher.News;
            }
            this.sb.Completed += new EventHandler(this.sb_Completed);
            this.showNewsListTimer = new DispatcherTimer();
            this.showNewsListTimer.Interval=TimeSpan.FromMilliseconds(5000.0);
            this.showNewsListTimer.Tick+=new EventHandler(this.showNewsListTimer_Tick);
            this.LayoutRoot.SizeChanged += new SizeChangedEventHandler(this.LayoutRoot_SizeChanged);
            base.Loaded+=new RoutedEventHandler(this.NewsControlsView_Loaded);
        }

        private void LayoutRoot_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if ((this.LayoutRoot.ActualHeight> 0.0) && (this.LayoutRoot.ActualWidth > 0.0))
            {
                double num;
                RectangleGeometry geometry = new RectangleGeometry();
                geometry.Rect=new Rect(0.0, 0.0, this.LayoutRoot.ActualWidth, this.LayoutRoot.ActualHeight);
                geometry.RadiusY=num = 10.0;
                geometry.RadiusX=num;
                this.LayoutRoot.SetValue(UIElement.ClipProperty, geometry);
            }
        }

        private void news_list_MouseEnter(object sender, MouseEventArgs e)
        {
            if (this._newsHeight > this.news_list.ActualHeight)
            {
                this.showNewsListTimer.Stop();
            }
        }

        private void news_list_MouseLeave(object sender, MouseEventArgs e)
        {
            if (this._newsHeight > this.news_list.ActualHeight)
            {
                this.showNewsListTimer.Start();
            }
        }

        private void news_list_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if ((this.news_list.ActualWidth > 0.0) && (this.news_list.ActualHeight> 0.0))
            {
                RectangleGeometry geometry = new RectangleGeometry();
                geometry.Rect=new Rect(0.0, 0.0, this.news_list.ActualWidth, this.news_list.ActualHeight);
                this.news_list.SetValue(UIElement.ClipProperty, geometry);
            }
        }

        private void NewsControlsView_Loaded(object sender, RoutedEventArgs e)
        {
            this._newsStackTop = 0.0;
            this._posItem = 0;
            if (this.stackNews.Children.Count == 0)
            {
                NewsItem item;
                this.stackNews.Children.Clear();
                foreach (Announment announment in this._annnounment)
                {
                    item = new NewsItem(announment);
                    item.Margin=new Thickness(0.0, 2.0, 0.0, 0.0);
                    this.stackNews.Children.Add(item);
                }
                this.stackNews.UpdateLayout();
                this._newsHeight = (double)this.stackNews.GetValue(FrameworkElement.ActualHeightProperty);
                if (this._newsHeight > this.news_list.ActualHeight)
                {
                    foreach (Announment announment in this._annnounment)
                    {
                        item = new NewsItem(announment);
                        item.Margin=new Thickness(0.0, 2.0, 0.0, 0.0);
                        this.stackNews.Children.Add(item);
                    }
                    this.showNewsListTimer.Start();
                }
                else
                {
                    this.showNewsListTimer.Stop();
                }
            }
        }

        private void sb_Completed(object sender, EventArgs e)
        {
            this.showNewsListTimer.Start();
        }

        private void showNewsListTimer_Tick(object sender, EventArgs e)
        {
            if (this.stackNews.Children.Count> 0)
            {
                if (-this._newsStackTop >= this._newsHeight)
                {
                    this._newsStackTop = 0.0;
                    this._posItem = 0;
                }
                if (this._posItem >= (this.stackNews.Children.Count - 1))
                {
                    this._posItem = 0;
                }
                this.sb.Stop();
                this.sb.Children.Clear();
                NewsItem item = this.stackNews.Children[this._posItem++] as NewsItem;
                DoubleAnimation animation = new DoubleAnimation();
                animation.From=new double?(this._newsStackTop);
                this._newsStackTop -= item.RenderSize.Height+ 2.0;
                animation.To=new double?(this._newsStackTop);
                animation.Duration = new Duration(TimeSpan.FromSeconds(item.RenderSize.Height / 100.0));
                Storyboard.SetTarget(animation, this.newsTransform);
                Storyboard.SetTargetProperty(animation, new PropertyPath("Y", new object[0]));
                this.sb.Children.Add(animation);
                this.sb.Begin();
                this.showNewsListTimer.Stop();
            }
        }
    }
}
